const AdminModel = require('../db/models/admin');

class AdminService {
    async createAdminAccount (adminAccount) {
        const username = adminAccount.username;
    
        const result = await AdminModel.findOne({
          where: { username }
        });
    
        if (result) {
            return await AdminModel.update(adminAccount, {
                where: { username }
            });
        } else {
            return await AdminModel.create(adminAccount);
        }
      }

      async login (userInfo) {
          const { username, password } = userInfo;

          const usernameExist = await AdminModel.findOne({
              where: { username }
          });

          if (!usernameExist) {
              return 10003;//账户不存在
          }

          const dbPassword = usernameExist.get('password');

          if (password !== dbPassword) {
              return 10004;//密码错误
          }

          const uid = usernameExist.get('id');

          return {//如果到了这里，那肯定是登陆成功了
              uid,
              username
          }
      }
}

module.exports = new AdminService()